<template>
  <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="发票编号" prop="invoiceCode">
            <el-input v-model="form.invoiceCode" placeholder="请输入发票编号"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="发票照片" prop="invoicePhoto">
            <file-upload-invoice v-model="form.invoicePhoto" @getInvoiceInfo="getInvoiceInfo"></file-upload-invoice>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {updateAction, getAction, getById, addAction, isEmpty, postAction} from '@/api/manage'

export default {
  name: "InvoiceUploadBatchForm",
  data() {
    return {
      title: "上传发票",
      open: false,
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: [],
      // 表单校验
      rules: {
        invoiceCode: [
          {required: true, message: "发票编号不能为空", trigger: "blur"}
        ],
        invoicePhoto: [
          {required: true, message: "发票照片不能为空", trigger: "blur"}
        ]
      },
      url: {
        getById: '/payApply/payApply/',
        add: '/payApply/payApply/add',
        batchUploadInvoice: '/payApply/payApply/batchUploadInvoice',
      },
      form: {
        invoiceCode: "",
      }
    };
  },
  methods: {
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.fileList = [];
      this.form = {}; // 添加: 重置详情数据
    },

    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addAction(this.url.batchUploadInvoice, this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.$emit('ok');
          });
        }
      });
    },

    handleBatchUpdate(ids) {
      this.reset();
      this.open = true;
      this.title = "批量上传发票";
      this.ids = ids;
      this.form.ids = ids.join(",");
    },

    getInvoiceInfo(info) {
      if (!isEmpty(this.form.invoiceCode)) {
        this.form.invoiceCode += ",";
      } else {
        this.form.invoiceCode = "";
      }
      this.form.invoiceCode += info.number;
    }

  }
};
</script>
